package com.tsky.yyj.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.yyj.entity.Area;
import com.tsky.yyj.entity.Seller;
import com.tsky.yyj.entity.Store;
import com.tsky.yyj.json.YYJJsonFormat;
import com.tsky.yyj.manager.BaseManager;

@Component("storeAction")
@Scope("prototype")
public class StoreAction extends BackAction {
	@Autowired
	private BaseManager entityManager;

	/* bean properties */
	private String ids;
	private String id;// 序号
	private String city;// @城市
	private String area;// @区域
	private String name;// 门店名（店铺名称）
	private String address;// 地址
	private String phone;// 座机（联系电话）
	private int ticket = 0;// 优惠劵数
	private String status = "0";// 审核状态（0：待审核、1：已审核）
	private String sellerid;// @所属商家
	
	public void grid() {
		QueryBean grid = new QueryBean();
		BeanUtil.copyProperties(grid, this);

		entityManager.query(grid, Store.class);

		String jsonData = YYJJsonFormat.storeToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}

	public void add() {
		Store entity = new Store();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		
		Seller pMenu = new Seller();
		if(!BeanUtil.isEmpty(sellerid)){
			pMenu = entityManager.getEntity(Seller.class, sellerid);
			entity.setSeller(pMenu);
		}
		
		Area cityObj = new Area();
		if(!BeanUtil.isEmpty(area)){
			cityObj = entityManager.getEntity(Area.class, area);
			entity.setArea(cityObj);
		}

		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Store.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Store entity = entityManager.getEntity(Store.class, id);
		ActionUtil.setToEL("storeEnt", entity);
		return "query";
	}

	public void update() {
		Store entity = entityManager.getEntity(Store.class, id);
		BeanUtil.copyProperties(entity, this);

		Seller pMenu = new Seller();
		if(!BeanUtil.isEmpty(sellerid)){
			pMenu = entityManager.getEntity(Seller.class, sellerid);
			entity.setSeller(pMenu);
		}else {
			entity.setSeller(null);
		}
		
		Area cityObj = new Area();
		if(!BeanUtil.isEmpty(area)){
			cityObj = entityManager.getEntity(Area.class, area);
			entity.setArea(cityObj);
		}
		
		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public BaseManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(BaseManager entityManager) {
		this.entityManager = entityManager;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public int getTicket() {
		return ticket;
	}

	public void setTicket(int ticket) {
		this.ticket = ticket;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getSellerid() {
		return sellerid;
	}

	public void setSellerid(String sellerid) {
		this.sellerid = sellerid;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}
}
